﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Reflection

Namespace System.Workflow.ComponentModel.Serialization
    Friend Class CollectionMarkupSerializer
        Inherits WorkflowMarkupSerializer
        ' Methods
        Protected Friend Overrides Sub AddChild(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal parentObj As Object, ByVal childObj As Object)
            If (parentObj Is Nothing) Then
                Throw New ArgumentNullException("parentObj")
            End If
            If Not CollectionMarkupSerializer.IsValidCollectionType(parentObj.GetType) Then
                Throw New Exception(SR.GetString("Error_SerializerTypeRequirement", New Object() { parentObj.GetType.FullName, GetType(ICollection).FullName, GetType(ICollection([Of] )).FullName }))
            End If
            parentObj.GetType.InvokeMember("Add", (BindingFlags.InvokeMethod Or (BindingFlags.Public Or BindingFlags.Instance)), Nothing, parentObj, New Object() { childObj }, CultureInfo.InvariantCulture)
        End Sub

        Protected Friend Overrides Sub ClearChildren(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If Not CollectionMarkupSerializer.IsValidCollectionType(obj.GetType) Then
                Throw New Exception(SR.GetString("Error_SerializerTypeRequirement", New Object() { obj.GetType.FullName, GetType(ICollection).FullName, GetType(ICollection([Of] )).FullName }))
            End If
            If Not TypeOf obj Is ICollection Then
                obj.GetType.InvokeMember("Clear", (BindingFlags.InvokeMethod Or (BindingFlags.Public Or BindingFlags.Instance)), Nothing, obj, New Object(0  - 1) {}, CultureInfo.InvariantCulture)
            End If
        End Sub

        Protected Friend Overrides Function GetChildren(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As IList
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If Not CollectionMarkupSerializer.IsValidCollectionType(obj.GetType) Then
                Throw New Exception(SR.GetString("Error_SerializerTypeRequirement", New Object() { obj.GetType.FullName, GetType(ICollection).FullName, GetType(ICollection([Of] )).FullName }))
            End If
            Dim enumerable As IEnumerable = [TryCast](obj,IEnumerable)
            Dim list As New ArrayList
            Dim obj2 As Object
            For Each obj2 In enumerable
                list.Add(obj2)
            Next
            Return list
        End Function

        Protected Friend Overrides Function GetProperties(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As PropertyInfo()
            Return New PropertyInfo(0  - 1) {}
        End Function

        Friend Shared Function IsValidCollectionType(ByVal collectionType As Type) As Boolean
            If (collectionType Is Nothing) Then
                Return False
            End If
            If GetType(Array).IsAssignableFrom(collectionType) Then
                Return False
            End If
            If Not GetType(ICollection).IsAssignableFrom(collectionType) Then
                If Not collectionType.IsGenericType Then
                    Return False
                End If
                If Not GetType(ICollection([Of] )).IsAssignableFrom(collectionType.GetGenericTypeDefinition) Then
                    Return GetType(IList([Of] )).IsAssignableFrom(collectionType.GetGenericTypeDefinition)
                End If
            End If
            Return True
        End Function

        Protected Friend Overrides Function ShouldSerializeValue(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal value As Object) As Boolean
            If (Not value Is Nothing) Then
                If Not CollectionMarkupSerializer.IsValidCollectionType(value.GetType) Then
                    Throw New Exception(SR.GetString("Error_SerializerTypeRequirement", New Object() { value.GetType.FullName, GetType(ICollection).FullName, GetType(ICollection([Of] )).FullName }))
                End If
                Dim enumerable As IEnumerable = [TryCast](value,IEnumerable)
                [Using] enumerator As IEnumerator = enumerable.GetEnumerator
                    Do While enumerator.MoveNext
                        Dim current As Object = enumerator.Current
                        Return True
                    Loop
                End [Using]
            End If
            Return False
        End Function

    End Class
End Namespace

